package TakeAwaySystem.adminoperation.admin;

import TakeAwaySystem.Admin;
import TakeAwaySystem.adminoperation.AdminIOperation;
import TakeAwaySystem.User;

import java.io.*;
import java.util.*;

import static TakeAwaySystem.Business.*;
import static TakeAwaySystem.User.NAME;

/**
 * @author Vv
 * @version 1.0
 * @project coding_learn
 * @description
 * @date 2023/9/26 22:29:40
 */
public class AdminModifyOperation implements AdminIOperation {
    @Override
    public void work(Admin o, File file) {
        System.out.println("修改当前登录的商户信息");
        o = (Admin) o;
        Scanner scanner = new Scanner(System.in);
        String strT = null;
        do{
            int choice = PrintModifyOperation();
            switch(choice){
                case 0:
                    break;
                case NAME :
                {
                    List<User> userList = fileToList(file);
                    Set<String> NameSet = new HashSet<>();
                    for(User user : userList){
                        NameSet.add(user.getName());
                    }
                    String newName = null;
                    do{
                        System.out.println("请输入你要修改的 AdminName.(注意名字不可以出现其他字符)");
                        newName = scanner.next();
                        if(newName.contains(" ")){
                            System.out.println("你设置的名字其中有空格 请重新输入");
                        }
                        if(NameSet.contains(newName)){
                            System.out.println("你设置的名字已经被其他管理员使用，请选择其他名字");
                        }
                    }while(newName.contains(" ")&& NameSet.contains(newName));
                    System.out.println("名字修改成功");
                }
                break;
                case PASSWORD :
                {
                    String password = null;
                    boolean isRight = true;
                    do{
                        //直到给我收到第一次密码合格 再进行重复测试
                        do{
                            System.out.println("现在请输入你要修改的密码");
                            password = scanner.next();
                            char[] str = password.toCharArray();
                            isRight = str.length >= 8 ? true : false;
                            System.out.print(isRight ? "密码长度合格" :"密码长度不合格");
                            for(int i = 0;str.length >= 8 && i<str.length;i++){
                                if(!(str[i] >= '0' && str[i] <= '9') && !(str[i] >= 'a' && str[i] <= 'z') && !(str[i] >= 'A' && str[i] <= 'Z')){
                                    System.out.println("出现了除大小写字母和数字 之外的字符");
                                    isRight = false;
                                }
                            }
                            System.out.println(isRight ? "密码合格" :"密码不合格");

                        }while(!isRight);
                        System.out.println("请重复输入密码 要求和上次输入的密码相同");
                        String pass2 = scanner.next();
                        isRight = pass2.equals(password);
                        System.out.println(isRight ? "密码正确 修改成功" : " 密码错误 重新输入");
                    }while(!isRight);
                    List<User> list = fileToList(file);
                    for(User user : list){
                        if(user.getId() == ((Admin) o).getId()){
                            ((Admin)user).setPassword(password);
                            break;
                        }
                    }
                    ListToFile(list,file);
                }
                break;
                default:
                    System.out.println("输入错误 请重新输入");
                    break;
            }
            System.out.println("请问你现在还需要继续修改么? 如果需要请输入YES 否则输入NO");
            strT = scanner.next();
        }while (strT.equals("YES"));
        System.out.println("现在退出");
    }
    private int PrintModifyOperation(){
        System.out.println("请输入你要修改的选项的数字");
        Scanner scanner = new Scanner(System.in);
        System.out.println("0.退出");
        System.out.println("1.business NAME");
        System.out.println("2.business PASSWORD");
        System.out.println("请输入相应的数字");
        return scanner.nextInt();
    }


    @Override
    public List<User> fileToList(File file)  {
        List<User> list = new ArrayList<>();
        //File file = new File("d:/Business.txt");
        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader(file);
            br = new BufferedReader(fr);
            while(true){
                String str = br.readLine();
                if(str != null){
                    String[] arr = str.split(" ");
                    Admin users = new Admin();
                    users.setId(Integer.valueOf(arr[0]));
                    users.setPassword(arr[1]);
                    users.setName(arr[2]);
                    list.add(users);
                }else{
                    break;
                }
            }
        }catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                fr.close();
                br.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        return list;
    }



    @Override
    public void ListToFile(List<User> list,File file) {
        // File file = new File("d:/Business.txt");
        FileWriter fileWriter = null;
        BufferedWriter bw = null;
        try {
            fileWriter = new FileWriter(file);
            bw = new BufferedWriter(fileWriter);
            int N = list.size();
            for(int i = 0;i<N;i++){
                Admin user = (Admin) list.get(i);
                StringBuilder sbr = new StringBuilder();
                sbr.append(user.getId() + " ");
                sbr.append(user.getPassword() + " ");
                sbr.append(user.getName() + " ");
                bw.write(sbr.toString());
                bw.newLine();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                bw.close();
                fileWriter.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }

    }
}
